package yl.hs.bmipfull.controllers.construction;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import yl.hs.bmipfull.controllers.ControllerBase;
import yl.hs.bmipfull.service.salemanage.ISaleOrderPlaceService;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.SubmitModel;
import yl.hs.bmipfull.viewmodels.WebResult;

/**
 * 作用:  下单表控制层<br/>
 * 创建时间: 2023年06月25日 10时36分42秒 <br/>
 *
 * @author 吴高丰
 */
@Api(value = "下单表Web接口", tags = "管理--下单表操作")
@RequestMapping(value = "/api/construction/saleorderplace")
@RestController
public class SaleOrderPlaceController extends ControllerBase {

    @Autowired
    ISaleOrderPlaceService service;

    @ApiOperation(value = "查询下单表", notes = "查询下单表")
    @RequestMapping(value = "", method = RequestMethod.POST)
    public WebResult queryList(@ApiParam(value = "查询条件") @RequestBody PageQuery pageQuery) {
        try {
            return success("查询成功", service.queryList(pageQuery));
        } catch (Exception e) {
            e.printStackTrace();
            return fail("查询失败,查询条件异常!");
        }
    }

    @ApiOperation(value = "查询下单表", notes = "分页查询下单表")
    @RequestMapping(value = "/{rowNum}/{page}", method = RequestMethod.POST)
    public WebResult queryPage(@RequestBody PageQuery query,
                               @ApiParam(value = "页码") @PathVariable int page,
                               @ApiParam(value = "每页条数") @PathVariable int rowNum) {
        try {
            var result = service.queryPageGD(query, rowNum, page);
            return success("ok", result);
        } catch (Exception ee) {
            return error(ee);
        }
    }
    @ApiOperation(value = "查询下单表", notes = "分页查询下单表")
    @RequestMapping(value = "/queryPlace", method = RequestMethod.GET)
    public WebResult queryPlace() {
        try {
            var result = service.queryPlace();
            return success("ok", result);
        } catch (Exception ee) {
            return error(ee);
        }
    }

    @ApiOperation(value = "查询下单表", notes = "根据下单表编号查询下单表")
    @RequestMapping(value = "/{sn}", method = RequestMethod.GET)
    public WebResult findBySn(@ApiParam(value = "编号") @PathVariable String sn) {
        return success("查询成功", service.findBySn(sn));
    }

    @ApiOperation(value = "查询下单明细表", notes = "查询下单明细表")
    @RequestMapping(value = "/detail/{sn}", method = RequestMethod.GET)
    public WebResult queryDetailBySn(@ApiParam(value = "编号") @PathVariable String sn) {
        return success("查询成功", service.queryDetailBySn(sn));
    }

    @ApiOperation(value = "保存下单表", notes = "查询/更新下单表信息")
    @RequestMapping(value = "", method = RequestMethod.PUT)
    public WebResult saveMenu(@ApiParam(value = "提交信息") @RequestBody SubmitModel submitModel) {
        Integer result;
        try {
            result = service.savesale_order_place(submitModel.getData(), submitModel.getSn());
            if (result > 0) {
                return success("保存成功");
            } else if (result == -1) {
                return fail("余额不足，请回款后重试");
            } else if (result == -2) {
                return fail("库存不足，无法下单");
            } else if (result == -3) {
                return fail("该提货单已审核");
            } else {
                return fail("数据未保存");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return fail("数据未保存");
        }

    }

    @ApiOperation(value = "删除下单表信息", notes = "根据下单表编号删除系下单表信息")
    @RequestMapping(path = "/delete/{sn}", method = RequestMethod.DELETE)
    public WebResult delete(@ApiParam(value = "下单表编号", name = "sn", allowMultiple = true, required = true) @PathVariable("sn") String sn) {
        if (service.delete(sn) > 0) {
            return success("删除成功");
        } else {
            return fail("删除失败");
        }
    }
}